package guanqu.server;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.alibaba.fastjson.JSON;

import guanqu.beans.IrrBDDeviceDetail;
import helpers.BeanHelper;
import manager.PersistentManager;

/**
 * Servlet implementation class GetDeviceBasicInfoByStatus
 */
//@WebServlet("/GetDeviceBasicInfoByStatus")
@Deprecated
public class GetDeviceBasicInfoByStatus extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetDeviceBasicInfoByStatus() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		response.getWriter().append("Served at: ").append(request.getContextPath());
		
		
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		
		String data = request.getParameter("data");
		Upload upload = JSON.parseObject(data,Upload.class);
		
		List<IrrBDDeviceDetail> irrBDDeviceDetails = new ArrayList<IrrBDDeviceDetail>();
		
		Download download = new Download();
		
		SessionFactory sf = PersistentManager.sharemanager().getSessionFactory();
//		Session session = sf.openSession();
		Session session = sf.getCurrentSession();
		
		Transaction tx = session.beginTransaction();
		try
		{
			Query q = session.createQuery("from IrrBDDeviceDetail dd where dd.devState = ?");
			q.setParameter(0, upload.getStatus());
			// 设置500条限制
			q.setMaxResults(500);
			
			irrBDDeviceDetails = q.list();
			download.setDetails(irrBDDeviceDetails);
			tx.commit();
			download.setStatus(DownloadInfo.SUCCESS);
		}catch(Exception e)
		{
			download.setStatus(DownloadInfo.FAILED);
			tx.rollback();
			e.printStackTrace();
		}
		finally {
//			session.close();
		}
		
		for(IrrBDDeviceDetail detail:irrBDDeviceDetails)
		{
			try {
				BeanHelper.beanAttributeValueTrim(detail);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		System.out.println(JSON.toJSONString(download));
		response.getWriter().append(JSON.toJSONString(download));
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
	
	// 下行数据
	public static class Download
	{
		int status;
		
		// 基本设备信息
		List<IrrBDDeviceDetail> details = new ArrayList<IrrBDDeviceDetail>();

		public int getStatus() {
			return status;
		}

		public void setStatus(int status) {
			this.status = status;
		}

		public List<IrrBDDeviceDetail> getDetails() {
			return details;
		}

		public void setDetails(List<IrrBDDeviceDetail> details) {
			this.details = details;
		}
		
		
		
	}
	
	
	// 上行数据
	public static class Upload
	{
		String status;

		public String getStatus() {
			return status;
		}

		public void setStatus(String status) {
			this.status = status;
		}

		
		
	}

}
